package com.goods.business.service.imp;

import com.goods.business.mapper.BizHealthMapper;
import com.goods.business.service.BizHealthService;
import com.goods.common.model.business.BizHealth;
import com.goods.common.model.system.User;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.HealthVO;
import com.goods.common.vo.system.PageVO;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BizHealthServiceImpl implements BizHealthService {

    @Autowired
    private BizHealthMapper bizHealthMapper;

    @Override
    public HealthVO checkIsReport(User user) {
        Example example = new Example(BizHealth.class);
        example.createCriteria().andEqualTo("userId", user.getId());
        List<BizHealth> bizHealths = bizHealthMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(bizHealths)){
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String today = simpleDateFormat.format(new Date()); //获取今天是哪一天

        for (BizHealth bizHealth : bizHealths) {
            Date createTime = bizHealth.getCreateTime();//获取签到的时间
            String format = simpleDateFormat.format(createTime); //签到的那天
            //判断是不是今天,如果是就返回今天的打卡记录,不是就继续往下判断，到最后为null
            if (today.equals(format)){
                HealthVO healthVO = new HealthVO();
                BeanUtils.copyProperties(bizHealth, healthVO);
                return healthVO;
            }
        }
        //到最后都没有匹配到，说明今天没有打卡，直接返回空
       return null;
    }

    @Override
    public void saveUserHealthStatus(HealthVO healthVO,User user) {
        BizHealth bizHealth = new BizHealth();
        BeanUtils.copyProperties(healthVO, bizHealth);
        bizHealth.setUserId(user.getId());
        bizHealth.setCreateTime(new Date());
        bizHealthMapper.insert(bizHealth);
    }

    @Override
    public PageVO<HealthVO> getHistoryByUser(Map<String,Object> queryMap,User user) {
        Example example = new Example(BizHealth.class);
        example.createCriteria().andEqualTo("userId", user.getId());
        List<BizHealth> bizHealths = bizHealthMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(bizHealths))
            return new PageVO<>(0, null);

        List<HealthVO> healthVOS = new ArrayList<>();
        for (BizHealth bizHealth : bizHealths) {
            HealthVO healthVO = new HealthVO();
            BeanUtils.copyProperties(bizHealth, healthVO);
            healthVOS.add(healthVO);
        }
        List<HealthVO> page =null;
        if (queryMap.get("pageNum") != null && queryMap.get("pageSize") != null) {
            page = ListPageUtils.page(healthVOS, Integer.parseInt((String) queryMap.get("pageSize")), Integer.parseInt((String) queryMap.get("pageNum")));
            return new PageVO<HealthVO>(bizHealths.size(),page);
        }
        return new PageVO<HealthVO>(bizHealths.size(),healthVOS);
    }

}
